﻿using System;
using System.Data;
using System.Windows.Forms;
using IntfClass;

namespace IntfSetting {
    public partial class FrmBuinessMain : Form {
        private DataTable datGridView = new DataTable();

        public FrmBuinessMain() {
            InitializeComponent();
        }

        private void btnCancle_Click(object sender, EventArgs e) {
            Dispose();
        }

        private void btnNew_Click(object sender, EventArgs e) {
            var frmBuinessNew = new FrmBuinessNew();
            frmBuinessNew.blnNew = true;
            frmBuinessNew.ShowDialog();
            SearchGridView();
        }

        private void SearchGridView() {
            var strSql = "select t.ID,t.code,t.name from TBusiness t";

            var classDB = new ClassDB();

            datGridView = classDB.ExecuteDataTable(strSql);
            DgvBusiness.DataSource = datGridView;
        }

        private void FrmClassMain_Load(object sender, EventArgs e) {
            SearchGridView();

            DgvBusiness.DataSource = datGridView;
            DgvBusiness.Columns[0].Visible = false;
            DgvBusiness.Columns[1].Visible = true;
            DgvBusiness.Columns[2].Visible = true;

            DgvBusiness.Columns[1].ReadOnly = true;
            DgvBusiness.Columns[2].ReadOnly = true;

            DgvBusiness.Columns[1].HeaderText = "编号";
            DgvBusiness.Columns[2].HeaderText = "名称";
        }

        private void btnUpt_Click(object sender, EventArgs e) {
            if (DgvBusiness.SelectedRows[0].Cells["ID"].Value == null) return;
            var frmBuinessNew = new FrmBuinessNew();
            frmBuinessNew.blnNew = false;
            frmBuinessNew.ID = Convert.ToInt64(DgvBusiness.SelectedRows[0].Cells["ID"].Value);
            frmBuinessNew.strCode = DgvBusiness.SelectedRows[0].Cells["Code"].Value.ToString();
            frmBuinessNew.strName = DgvBusiness.SelectedRows[0].Cells["ID"].Value.ToString();

            frmBuinessNew.ShowDialog();
            SearchGridView();
        }

        private void btnDel_Click(object sender, EventArgs e) {
            if (DgvBusiness.SelectedRows[0].Cells["ID"].Value == null) return;

            var dialogResult = MessageBox.Show("确认删除" + DgvBusiness.SelectedRows[0].Cells["Code"].Value + "?",
                Text, MessageBoxButtons.OKCancel);
            if (dialogResult == DialogResult.OK) {
                //删除
                var strID = DgvBusiness.SelectedRows[0].Cells["ID"].Value.ToString();

                var strSql = "delete from TBusiness where ID =" + strID + " ; ";

                var classDB = new ClassDB();

                classDB.ExecuteTransaction(strSql);
            }

            SearchGridView();
        }
    }
}